﻿import pygame
from pygame.locals import *

import Properties
from widgets import *
from Primitives import *
from Entities import *
from Units import Snake

class EventManager(Primitives.EventManager):

	def fireEvent(self, scene,go, event ):
		'''
		if event.type == QUIT:
			#pass
			scene.Snake.print_debug()
		elif event.type == KEYDOWN and event.key == K_RETURN:		
			scene.Snake._is_visible = True
			scene.Curve._is_visible = False
			
			scene.Snake.start()				

		elif event.type == KEYDOWN and event.key == K_SPACE:
			
			if scene.Snake.is_looping():		
				scene.Snake.stop()
			else:
				scene.Snake.restart()
			
		elif event.type == MOUSEBUTTONDOWN:
			scene.Snake._is_visible = False
			scene.Curve._is_visible = True
			
			scene.Curve.go_draw();
			
		elif event.type == MOUSEBUTTONUP:
			scene.Curve.stop_draw()	
			scene.Snake.set_curve( scene.Curve )
			print scene.Curve.unit.print_points()
			
		elif  event.type == KEYDOWN and event.key == K_BACKSPACE:
			scene.Snake.go_back(-1)	

		elif  event.type == KEYDOWN and event.key == K_a:				
			scene.Snake.set_manual_alfa()		
									
		elif  event.type == KEYDOWN and event.key == K_b:				
			scene.Snake.set_manual_beta()			
				
		elif  event.type == KEYDOWN and event.key == K_g:				
			scene.Snake.set_manual_gama()												
													
		elif  event.type == KEYDOWN and event.key == K_d:				
			scene.Snake.set_manual_delta()		
			
		elif event.type == KEYDOWN and event.key == K_l:
			scene.Snake._show_dir_pt_linear = not scene.Snake._show_dir_pt_linear
			
		elif event.type == KEYDOWN and event.key == K_m:
			scene.Snake.toogle_mode()	
																			
		elif  event.type == KEYDOWN:
			scene.Snake.input( event.unicode )
		'''
		pass

class InitialScene(Scene):
	
	def __init__(self):
		backSurface = pygame.Surface( (950,500) )
		backSurface = backSurface.convert()
		backSurface.fill((255, 255, 255))
		
		objects = []
		
		self._create_scene(objects)	
	
		super(InitialScene,self).__init__( backSurface,objects)
		
	def _create_scene(self,objects):
		textRect = pygame.Rect( (20,20), (200,30) )
		#textRect.centerx = backSurface.get_rect().centerx
		textColor = (0,0,0)
		font = pygame.font.Font(None,24)
		text = font.render( "Desenhe uma curva inicial", 1, textColor )
		
		textSpr = GameObject("Title",EventManager())
		textSpr.image = text
		textSpr.rect = textRect
		
		curve = CurveEntitie("Curve")
		#curva.rect.centerx = backSurface.get_rect().centerx
		curve.rect.y = 60
	
		snake = SnakeEntitie("Snake")
		snake._is_visible = False	
				
		objects.append( textSpr )
		objects.append( curve )
		objects.append( snake )	
		
		x_offset = 20
		y_offset = 435
		
		btnPlay = Button("btnPlay",self.play)
		btnPlay.label = "Iniciar"
		btnPlay.rect.x = 400 + x_offset
		btnPlay.rect.y = y_offset
		btnPlay.pack()		

		btnBefore = Button("btnBefore",self.before)
		btnBefore.label = "Passo Anterior"
		btnBefore.rect.x = btnPlay.rect.x + btnPlay.rect.w + x_offset
		btnBefore.rect.y = y_offset
		btnBefore.pack()	
		
		btnNext = Button("btnNext",self.next)
		btnNext.label = "Proximo Passo"
		btnNext.rect.x = btnBefore.rect.x + btnBefore.rect.w + x_offset
		btnNext.rect.y = y_offset
		btnNext.pack()			
		
		btnRestart = Button("btnRestart",self.restart)
		btnRestart.label = "Reiniciar"
		btnRestart.rect.x = btnNext.rect.x + btnNext.rect.w + x_offset
		btnRestart.rect.y = y_offset
		btnRestart.pack()			
		
		objects.append( btnPlay )	
		objects.append( btnNext )	
		objects.append( btnBefore )	
		objects.append( btnRestart )	
		
		lblPtSelected = Label("lblPtSelected")
		lblPtSelected.label = "Ponto Selecionado: "
		lblPtSelected.rect.x = 380 + x_offset
		lblPtSelected.rect.y = 60	
		lblPtSelected.pack()
		
		lblPtDirection = Label("lblPtDirection")
		lblPtDirection.label = "Direcao: "
		lblPtDirection.rect.x = 380 + x_offset
		lblPtDirection.rect.y = 100	
		lblPtDirection.pack()		
		
		lblPtLinearDirection = Label("lblPtLinearDirection")
		lblPtLinearDirection.label = "Direcao Linear: "
		lblPtLinearDirection.rect.x = 380 + x_offset
		lblPtLinearDirection.rect.y = 120	
		lblPtLinearDirection.pack()			
		
		lblPtContinuity = Label("lblPtContinuity")
		lblPtContinuity.label = "Continuidade: "
		lblPtContinuity.rect.x = 380 + x_offset
		lblPtContinuity.rect.y = 140	
		lblPtContinuity.pack()			

		lblPtSuavity = Label("lblPtSuavity")
		lblPtSuavity.label = "Suavidade: "
		lblPtSuavity.rect.x = 380 + x_offset
		lblPtSuavity.rect.y = 160	
		lblPtSuavity.pack()			
		
		lblPtColor = Label("lblPtColor")
		lblPtColor.label = "Cor: "
		lblPtColor.rect.x = 380 + x_offset
		lblPtColor.rect.y = 180
		lblPtColor.pack()			
		
		lblPtGradContinuity = Label("lblPtGradContinuity")
		lblPtGradContinuity.label = "Grad Cont: "
		lblPtGradContinuity.rect.x = 380 + x_offset
		lblPtGradContinuity.rect.y = 200	
		lblPtGradContinuity.pack()			

		lblPtGradSuavity = Label("lblPtGradSuavity")
		lblPtGradSuavity.label = "Grad Suav: "
		lblPtGradSuavity.rect.x = 380 + x_offset
		lblPtGradSuavity.rect.y = 220	
		lblPtGradSuavity.pack()		
		
		lblPtGradColor = Label("lblPtGradColor")
		lblPtGradColor.label = "Grad Cor: "
		lblPtGradColor.rect.x = 380 + x_offset
		lblPtGradColor.rect.y = 240	
		lblPtGradColor.pack()							
		
		lblPtAlfa = Label("lblPtAlfa")
		lblPtAlfa.label = "Alfa: "
		lblPtAlfa.rect.x = 380 + x_offset
		lblPtAlfa.rect.y = 260	
		lblPtAlfa.pack()			

		lblPtBeta = Label("lblPtBeta")
		lblPtBeta.label = "Beta: "
		lblPtBeta.rect.x = 380 + x_offset
		lblPtBeta.rect.y = 280	
		lblPtBeta.pack()				
		
		lblPtGama = Label("lblPtGama")
		lblPtGama.label = "Gama: "
		lblPtGama.rect.x = 380 + x_offset
		lblPtGama.rect.y = 300	
		lblPtGama.pack()
		
		objects.append(lblPtSelected)	#7
		objects.append(lblPtDirection)
		objects.append(lblPtLinearDirection)
		objects.append(lblPtContinuity)
		objects.append(lblPtSuavity)
		objects.append(lblPtColor)
		objects.append(lblPtGradContinuity)
		objects.append(lblPtGradSuavity)
		objects.append(lblPtGradColor)
		objects.append(lblPtAlfa)
		objects.append(lblPtBeta)
		objects.append(lblPtGama)
		
		
		for i in range(7,19):
			objects[i].max_w = lblPtSelected.rect.w		
		
		
		lblIteracao = Label("lblIteracao")
		lblIteracao.label = "Iteracao: "
		lblIteracao.rect.x = 650 + x_offset
		lblIteracao.rect.y = 60	
		lblIteracao.pack()
		
		lblFuncional = Label("lblFuncional")
		lblFuncional.label = "Funcional: "
		lblFuncional.rect.x = 650 + x_offset
		lblFuncional.rect.y = 100	
		lblFuncional.pack()

		lblContinuity = Label("lblContinuity")
		lblContinuity.label = "Continuidade: "
		lblContinuity.rect.x = 650 + x_offset
		lblContinuity.rect.y = 120	
		lblContinuity.pack()		
		
		lblSuavity = Label("lblSuavity")
		lblSuavity.label = "Suavidade: "
		lblSuavity.rect.x = 650 + x_offset
		lblSuavity.rect.y = 140	
		lblSuavity.pack()		
		
		lblColor = Label("lblColor")
		lblColor.label = "Cor: "
		lblColor.rect.x = 650 + x_offset
		lblColor.rect.y = 160	
		lblColor.pack()			

		lblLambda = Label("lblLambda")
		lblLambda.label = "Lambda: "
		lblLambda.rect.x = 650 + x_offset
		lblLambda.rect.y = 180	
		lblLambda.pack()				
		
		objects.append(lblIteracao) #18
		objects.append(lblFuncional)
		objects.append(lblContinuity)
		objects.append(lblSuavity)
		objects.append(lblColor)
		objects.append(lblLambda)
		

		for i in range(19,25):
			objects[i].max_w = lblContinuity.rect.w		
		
		
		
		lblValuePtSelected = Label("lblValuePtSelected")
		lblValuePtSelected.label = "0"
		lblValuePtSelected.rect.x = 510 + x_offset
		lblValuePtSelected.rect.y = 60	
		lblValuePtSelected.pack()
		
		lblValuePtDirection = Label("lblValuePtDirection")
		lblValuePtDirection.label = "0"
		lblValuePtDirection.rect.x = 510 + x_offset
		lblValuePtDirection.rect.y = 100	
		lblValuePtDirection.pack()		
		
		lblValuePtLinearDirection = Label("lblValuePtLinearDirection")
		lblValuePtLinearDirection.label = "0"
		lblValuePtLinearDirection.rect.x = 510 + x_offset
		lblValuePtLinearDirection.rect.y = 120	
		lblValuePtLinearDirection.pack()				
		
		lblValuePtContinuity = Label("lblValuePtContinuity")
		lblValuePtContinuity.label = "0"
		lblValuePtContinuity.rect.x = 510 + x_offset
		lblValuePtContinuity.rect.y = 140	
		lblValuePtContinuity.pack()			

		lblValuePtSuavity = Label("lblValuePtSuavity")
		lblValuePtSuavity.label = "0"
		lblValuePtSuavity.rect.x = 510 + x_offset
		lblValuePtSuavity.rect.y = 160	
		lblValuePtSuavity.pack()			
		
		lblValuePtColor = Label("lblValuePtColor")
		lblValuePtColor.label = "0"
		lblValuePtColor.rect.x = 510 + x_offset
		lblValuePtColor.rect.y = 180
		lblValuePtColor.pack()	

		lblValuePtGradContinuity = Label("lblValuePtGradContinuity")
		lblValuePtGradContinuity.label = "0"
		lblValuePtGradContinuity.rect.x = 510 + x_offset
		lblValuePtGradContinuity.rect.y = 200
		lblValuePtGradContinuity.pack()		

		lblValuePtGradSuavity = Label("lblValuePtGradSuavity")
		lblValuePtGradSuavity.label = "0"
		lblValuePtGradSuavity.rect.x = 510 + x_offset
		lblValuePtGradSuavity.rect.y = 220
		lblValuePtGradSuavity.pack()		

		lblValuePtGradColor = Label("lblValuePtGradColor")
		lblValuePtGradColor.label = "0"
		lblValuePtGradColor.rect.x = 510 + x_offset
		lblValuePtGradColor.rect.y = 240
		lblValuePtGradColor.pack()			
		
		lblValuePtAlfa = Label("lblValuePtAlfa")
		lblValuePtAlfa.label = "0"
		lblValuePtAlfa.rect.x = 510 + x_offset
		lblValuePtAlfa.rect.y = 260	
		lblValuePtAlfa.pack()			

		lblValuePtBeta = Label("lblValuePtBeta")
		lblValuePtBeta.label = "0"
		lblValuePtBeta.rect.x = 510 + x_offset
		lblValuePtBeta.rect.y = 280	
		lblValuePtBeta.pack()				
		
		lblValuePtGama = Label("lblValuePtGama")
		lblValuePtGama.label = "0"
		lblValuePtGama.rect.x = 510 + x_offset
		lblValuePtGama.rect.y = 300	
		lblValuePtGama.pack()
		
		objects.append(lblValuePtSelected)
		objects.append(lblValuePtDirection)
		objects.append(lblValuePtLinearDirection)
		objects.append(lblValuePtContinuity)
		objects.append(lblValuePtSuavity)
		objects.append(lblValuePtColor)
		objects.append(lblValuePtGradContinuity)
		objects.append(lblValuePtGradSuavity)
		objects.append(lblValuePtGradColor)		
		objects.append(lblValuePtAlfa)
		objects.append(lblValuePtBeta)
		objects.append(lblValuePtGama)
		
		lblValueIteracao = Label("lblValueIteracao")
		lblValueIteracao.label = "0"
		lblValueIteracao.rect.x = 750 + x_offset
		lblValueIteracao.rect.y = 60	
		lblValueIteracao.pack()
		
		lblValueFuncional = Label("lblValueFuncional")
		lblValueFuncional.label = "0"
		lblValueFuncional.rect.x = 750 + x_offset
		lblValueFuncional.rect.y = 100	
		lblValueFuncional.pack()

		lblValueContinuity = Label("lblValueContinuity")
		lblValueContinuity.label = "0"
		lblValueContinuity.rect.x = 750 + x_offset
		lblValueContinuity.rect.y = 120	
		lblValueContinuity.pack()		
		
		lblValueSuavity = Label("lblValueSuavity")
		lblValueSuavity.label = "0"
		lblValueSuavity.rect.x = 750 + x_offset
		lblValueSuavity.rect.y = 140	
		lblValueSuavity.pack()		
		
		lblValueColor = Label("lblValueColor")
		lblValueColor.label = "0"
		lblValueColor.rect.x = 750 + x_offset
		lblValueColor.rect.y = 160	
		lblValueColor.pack()			

		lblValueLambda = Label("lblValueLambda")
		lblValueLambda.label = "0"
		lblValueLambda.rect.x = 750 + x_offset
		lblValueLambda.rect.y = 180	
		lblValueLambda.pack()				
		
		objects.append(lblValueIteracao) #15
		objects.append(lblValueFuncional)
		objects.append(lblValueContinuity)
		objects.append(lblValueSuavity)
		objects.append(lblValueColor)
		objects.append(lblValueLambda)		
		
		
	def loop_routine(self):
		if self.Snake.is_started():
			self.Snake.minimize()
			
	def play(self,go):
		self.Snake._mode = SnakeEntitie.CONTINUO
		if self.Snake.is_started():
			if self.Snake.is_looping():		
				go.label = "Iniciar"
				go.pack()
				
				self.Snake.pause()
			else:				
				go.label = "Pausar"
				go.pack()								
				self.Snake.restart()		
		else:
			self.Snake._is_visible = True
			self.Curve._is_visible = False
			
			self.Snake.start()		
			
			go.label = "Pausar"
			go.pack()					
		
	def next(self,go):
		self.Snake._mode = SnakeEntitie.STEP_BY_STEP
		self.Snake.restart()

	def before(self,go):
		self.Snake._mode = SnakeEntitie.STEP_BY_STEP
		self.Snake.go_back(-1)

	def restart(self,go):
		self.Snake.stop()		
			
			
